package com.renwei.yunlong.activity.news;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.MsgDetailBean;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.LogUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.renwei.yunlong.view.SimpleOptionView;
import com.renwei.yunlong.view.TypesetTextView;
import com.renwei.yunlong.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MessageDetailActivity extends BaseActivity implements HttpTaskListener {

    @Bind(R.id.simpleTileView)
    SimpleOptionView simpleTileView;
    @Bind(R.id.tv_people)
    TextView tvPeople;
    @Bind(R.id.tv_type)
    TextView tvType;
    @Bind(R.id.tv_time)
    TextView tvTime;
    @Bind(R.id.tv_content)
    TypesetTextView tvContent;
    private int id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_detail);
        LogUtil.e("MessageDetailActivity");
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);

        id = getIntent().getIntExtra("id", 0);
        simpleTileView.setTitle("提醒详情");
        initData();
    }

    private void initData() {
        HashMap<String, String> map = new HashMap<>();
        map.put("messageId", id + "");
        ServiceRequestManager.getManager().getMessageDetail(this, JsonMapUtil.mapToJson(map), this);
    }

    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId) {
            case 2:
                MsgDetailBean bean = new Gson().fromJson(result, MsgDetailBean.class);
                if (bean.getMessage().getCode() == 200) {
                    String name = "";
                    if (!TextUtils.isEmpty(bean.getRows().getSenUserName())) {
                        name = name + bean.getRows().getSenUserName();
                    }
                    if (!TextUtils.isEmpty(bean.getRows().getMobilePhone())) {
                        name = name + "/" + bean.getRows().getMobilePhone();
                    }
                    tvPeople.setText(name);
                    tvType.setText(bean.getRows().getType());
                    if (!TextUtils.isEmpty(bean.getRows().getCreateDate())) {
                        tvTime.setText(bean.getRows().getCreateDate().replace("T", " "));
                    }
                    tvContent.setText(bean.getRows().getContent());
                } else {
                    showCenterInfoMsg(bean.getMessage().getMessage());
                }
                break;
            default:
                break;
        }
    }

    @Override    public void onException(int requestId, String code) {        showTopWrongMsg(getResources().getString(R.string.net_work_error));    }

    public static void OpenActivity(Context context, int messageId) {
        Intent intent = new Intent(context, MessageDetailActivity.class);
        if (context instanceof Application){
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        intent.putExtra("id", messageId);
        context.startActivity(intent);
    }

}
